package com.bigcat.gulimall.demo.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加密工具类
 */
//@Component
public class AESUtil {

    private transient Cipher eCipher;
    private transient Cipher dCipher;

    private final String key;
    /**
     * 加密秘钥
     */
    private static final String SECRET_KEY = "e1780b101e0e075c";
    /**
     * 信任登录秘钥
     */
    private static final String TRUST_KEY = "b8bd94f63a839961";

    public AESUtil(String key) {
        this.key = key;
    }

    /**
     * 加密方法
     *
     * @param original
     * @return
     */
    public String encrypt(String original) {
        if (StringUtils.isEmpty(original)) return "";
        try {
            initChipher();
            return new String(Base64.encodeBase64(eCipher.doFinal(original.getBytes())));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 解密方法
     *
     * @param original
     * @return
     */
    public String decrypt(String original) {
        if (StringUtils.isEmpty(original)) return "";
        try {
            initChipher();
            return new String(dCipher.doFinal(Base64.decodeBase64(original.getBytes())));
        } catch (Exception e) {
            return "";
        }
    }

    private void initChipher() throws Exception {
        if (eCipher != null || dCipher != null) {
            return;
        }
        synchronized (this) {
            if (eCipher != null || dCipher != null) {
                return;
            }
            SecretKeySpec sKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            eCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
            eCipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
            dCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
            dCipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        }
    }

    /**
     * 签名生成
     *http://download.dingdatech.com/router.html?citycode=57109&sn=8888008&biztype=1
     * @return
     */
    public String generateSign() {
        AESUtil aesUtil = new AESUtil(SECRET_KEY);
        StringBuffer str = new StringBuffer();
        String accessToken = "dIoY+ulf2btTjK/HAfxU198xmkLwXv5d29aRkXc5g2YIerWXNcixkFzEPC64YLV7";
        String serviceId = aesUtil.encrypt("43");
        String appId = aesUtil.encrypt("mtadc11ab5c61800816");
        String terminalType = aesUtil.encrypt("12");
        String requestType = aesUtil.encrypt("0");
        String deviceId = aesUtil.encrypt("9999");
        String parkNum = aesUtil.encrypt("005");

        str.append("accessToken=").append(accessToken);
        str.append("&serviceId=").append(serviceId);
        str.append("&appId=").append(appId);
        str.append("&terminalType=").append(terminalType);
        str.append("&requestType=").append(requestType);
        str.append("&DeviceId=").append(deviceId);
        str.append("&parkNum=").append(parkNum);
        char[] chars = Hex.encodeHex(DigestUtils.md5(str.toString()));
        return new String(chars);
    }

    public static void main(String[] args) {
        //加密
        AESUtil aesUtil = new AESUtil(SECRET_KEY);
        String encrypt = aesUtil.encrypt("ASE100001");
        String decrypt = aesUtil.decrypt("NfKZt7UO1UjZl6Qrt9xqaQ==");
        System.out.println("加密后数据：" + encrypt + "解密后数据：" + decrypt);

        //签名
        String sign = aesUtil.generateSign();
        System.out.println("签名：" + sign);

        //生成信任登录token
        AESUtil aesUtil1 = new AESUtil(TRUST_KEY);
        String loginName = "17696766245";
        String terminalId = "dajia";
        String trustToken = aesUtil1.encrypt(terminalId + "@" + loginName);

        System.out.println("信任访问令牌：" + trustToken);

        aesUtil.encrypt("u01290765," + "f23a57ca," + System.currentTimeMillis());

    }

}
